﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using Telerik.WebControls;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Upload;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;

public partial class Theme_Index_AddTheme_Classs : PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.Web.BBS.ManagerBBS opBBS = new PowerCreator.LMS.Web.BBS.ManagerBBS();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private string newFIleName = string.Empty;
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected string itype = "0";
    protected string domainID = "0";
    protected string classID = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        this.Title = "班级互动讨论区";

        classID = Request.QueryString["ClassID"].ToString();
        ViewState["ClassID"] = classID;
        voClass = objClass.Load(int.Parse(classID));
        lbl_ClassName.Text = voClass.Name;

        ViewState["DomainID"] = "0";
        domainID = Request.QueryString["DomainID"].ToString();
        ViewState["DomainID"] = domainID;

        ViewState["Type"] = "0";
        itype = Request.QueryString["Type"].ToString();
        ViewState["Type"] = itype;
        if (!IsPostBack)
        {
            //BinderRoundTheme();
            //BinderPageData();
            Binderdpdlst_AccessoryType();
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    protected override void InitPage()
    {
        //Button1.Text = GetLanageValueByKey("btn_EditResourceInfo_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_DomainName.Text = GetLanageValueByKey("theme_BBS_NoDomainName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_tishi.Text = GetLanageValueByKey("theme_BBS_TitleMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    //private void BinderRoundTheme()
    //{
    //    //随机推荐
    //    string search = string.Format("ThemeIsVisible = 1 ");
    //    if (ViewState["DomainID"].ToString() != "0")
    //    {
    //        search += " and ThemeDomainID = " + ViewState["DomainID"] ;
    //    }

    //    string orderList = "  newid() ";
    //    DataTable dt = opBBS.QueryTheme(search, orderList, PowerCreator.LMS.Web.Config.WebConfig.PageSize, 1);
    //    rpt_RoundTheme.DataSource = dt;
    //    rpt_RoundTheme.DataBind();
    //}
    //private void BinderPageData()
    //{
    //    PowerCreator.LMS.BBS.Entity.VODomain voDomain = opBBS.LoadDomain(int.Parse(ViewState["DomainID"].ToString()));
    //    if (voDomain.DomainID > 0)
    //    {
    //        lbl_DomainName.Text = voDomain.DomainName;
    //    }
    //    else
    //    {
    //        lbl_DomainName.Text ="未选择";
    //    }
    //}
    private void Binderdpdlst_AccessoryType()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "-1";
        dpdlst_AccessoryType.Items.Add(li);

        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("theme_center_ResourceStat_Video", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "0";
        dpdlst_AccessoryType.Items.Add(li1);

        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("theme_center_ResourceStat_OtherResource", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "1";
        dpdlst_AccessoryType.Items.Add(li2);
    }
    private bool CheckPageData()
    {
        if (string.IsNullOrEmpty(txt_ThemeTitle.Text.Trim()))
        {
            Script.Alert(Page, "update", config.GetLanagerValueByKey("web_bbs_EditTitleInfo_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (txt_ThemeTitle.Text.Length > 30)
        {
            Script.Alert(Page, "msg", config.GetLanagerValueByKey("web_bbs_EditTitleInfo_msg_m4", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.Alert(Page, "msg", config.GetLanagerValueByKey("theme_Theme_AddTheme_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }

    /// <summary>
    ///  获取上传资源时文件夹路径
    /// </summary>
    /// <param name="courseID">课程ID</param>
    /// <param name="srt">资源类型</param>
    /// <returns></returns>
    public string GetFileName()
    {
        Random objRand = new Random();
        System.DateTime date = DateTime.Now;
        //生成随机文件名
        string saveName = date.ToString("yyyyMMddHHmmsss") + Convert.ToString(objRand.Next(99) * 97 + 100);

        return saveName;
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;


        string fileName = string.Empty;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Path = Server.MapPath("../../fileManager/UploadFile/BBS/");

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            //组合路径，file.GetName()取得文件名
            fileName = GetFileName() + file.GetExtension();

            Path = Path + fileName;

            //保存
            file.SaveAs(Path, true);
            int filesize = file.ContentLength;

            //string url = "AddDownload.aspx?AddDownload=upload&DownloadID=" + ViewState["DownloadID"].ToString() + "&fullpath=fileManager/downloadFile/" + fileName + "&filesize=" + filesize;
            //Script.RedirectURL(url);
        }

        PowerCreator.LMS.BBS.Entity.VOTheme voTheme = new PowerCreator.LMS.BBS.Entity.VOTheme();
        voTheme.ThemeContent = txt_Description.Text;
        voTheme.ThemeCreatetime = DateTime.Now;
        voTheme.ThemeDomainID = int.Parse(ViewState["DomainID"].ToString());
        voTheme.ThemeType = int.Parse(ViewState["Type"].ToString());
        voTheme.ThemeIsElite = 0;
        voTheme.ThemeIsRecommend = 0;
        voTheme.ThemeIsTop = 0;
        voTheme.ThemeIsVisible = 1;
        voTheme.ThemeLastRevertTime = DateTime.Now;
        voTheme.ThemeReversionNum = 0;
        voTheme.ThemeTitle = txt_ThemeTitle.Text;
        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];

        voTheme.ThemeUserID = login.UserID;
        voTheme.ThemeUserName = login.RealName;
        voTheme.ThemeVisitNum = 0;

        voTheme.AccessoryName = txt_AccessoryName.Text;
        voTheme.AccessoryRUL = fileName;

        voTheme.AccessoryType = int.Parse(dpdlst_AccessoryType.SelectedValue);
        if (opBBS.CreateTheme(voTheme))
        {
            string url = "BBS_Class.aspx?Type=" + ViewState["Type"].ToString() + "&DomainID=" + ViewState["DomainID"].ToString() + "&ClassID=" + ViewState["ClassID"].ToString() + "&CourselistID=" + ViewState["DomainID"].ToString();
            Script.RedirectURL(url);
        }
    }
}
